package cn.jbit.stream.fastjson;

import cn.jbit.stream.pojo.User;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

public class TestMapSerialize {


    @Test
    public  void  testMapSerial(){

        Map<String,String> maps=new HashMap<>();

        maps.put("name","zhangjie");
        maps.put("sex","123.com");
        maps.put("age","20");

        //通过fastjson进行一个反序列化
        String s = JSONArray.toJSONString(maps);
        System.out.println(s);
    }


    @Test
    public  void testUnSerialMap(){

         String  json="{\"name\":\"张三\",\"sex\":\"29\",\"age\":\"29\"}";
         //反序列化map集合
        Map<String,String> map = JSONArray.parseObject(json, Map.class);
        System.out.println(map);
        map.forEach((k,v)-> System.out.println(k));

    }

    /**
     * 转换复杂对象
     */
    @Test
    public  void testUnSerialOb(){
        String  json="{\"user\":{\"id\":\"1\",\"name\":\"张杰\",\"password\":\"123.com\"}}";

        Map<String, User> stringObjectMap = JSONArray.parseObject(json, new TypeReference<Map<String, User>>() {
        });
        stringObjectMap.forEach((k,v)-> System.out.println(v.getClass().getName()));

    }
}
